import store from './store.js'
const fun={
	
	newfun:function(){
		let fun={}
		let http="https://loveioo.com/102/xcxmusic/"
		fun.checklogin=function(){
			let userid=99
			if(userid){
				return true;
			}else{
				return false;
			}
		}
		
		fun.login=function(){
				return new Promise((resolve,reject)=>{
					uni.login({
					  provider: 'weixin',
					  success: function (res) {
					    console.log(res);
					    // 获取用户信息
					    uni.getUserInfo({
					      provider: 'weixin',
					      success: function (res2) {
					        console.log(res2);
							uni.showLoading({
								title:"登录中..."
							})
							uni.request({
								url:'https://loveioo.com/102/xcxmusic/php/xcx_login.php',
								method:'POST',
								header: {
									'content-type': 'application/x-www-form-urlencoded' //自定义请求头信息
								},
								data:{
									code:res.code,
									iv:res2.iv,
									encryptedData:res2.encryptedData
								},
								success: (res3) => {
									console.log(res3)
									let aa={
										"userid":res3.data[0].userid,
										"openid":res3.data[0].openid,
										"nickname":res3.data[0].nickname,
										"stu_name":res3.data[0].stu_name,
										"avatarurl":res3.data[0].avatarurl
									}
									uni.hideLoading()
									uni.setStorageSync("userInfo",aa)
									uni.navigateBack({
									    delta: 1
									});
									resolve(res3)
									//return res3;
								},
								fail:(err)=>{
									console.log(err)
									fun.toast('登录失败')
									reject(err)
								}
							})
					      },
						  fail: (err) => {
						  	console.log(err)
							reject(err)
						  }
					    });
					  }
					});
				})
		}
		fun.toast=function (title,icon = "none",mask,duration=2000) {
			console.log(mask)
			uni.showToast({ 
				title:title,
				icon:icon,
				mask:true,
				duration:duration,
				
			});
		}
		fun.modal=function (content, showCancel = false,success) {
			uni.showModal({
				title: '提示',
				content:content,
				showCancel:showCancel,
				cancelColor:'#c8c8c8',
				success: (e) => {
					if(e.confirm){
						if(success){
							success()
						}
					}else{
						
					}
				}
			});
		}
		fun.getClassInfobyId=function(wkid){//根据班级id获取班级信息
			return new Promise(function(resolve,reject){
				fun.request({wkid:wkid},'php/xcx_getoneclassinfo.php').then(res=>{
					console.log(res)
					if(res.data.length>0){
						let mm=res.data[0]
						if(mm.wk_classImg){mm.wk_classImg=http + mm.wk_classImg}
						if(mm.classNoteImg){mm.classNoteImg=http + mm.classNoteImg}
						store.state.classInfo=mm
						//console.log(mm)
					}
					resolve(res)
				}).catch(err=>{
					reject(err)
				})
			})
		},
		fun.request=function(params,url){
			return new Promise((resolve,reject)=>{
				uni.request({
					url:http + url, //仅为示例，并非真实接口地址。
					data:params,
					method:'POST',
					header: {
						'content-type': 'application/x-www-form-urlencoded' //自定义请求头信息
					},
					success: (res) => {
						resolve(res)
					},
					fail: (err) => {
						reject(err)
					}
				})
			})
		}
		
		
		
		
		
		
		return fun
	},
	
	
	
	
	
}
export default fun